package com.sikadwafastdaisfondturbo.pretloan.utils.dialog

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.sikadwa.analysis.AnalysisBuilder
import com.sikadwafastdaisfondturbo.pretloan.R
import com.sikadwafastdaisfondturbo.pretloan.app.App
import com.sikadwafastdaisfondturbo.pretloan.databinding.DialogPersonalBinding
import com.sikadwafastdaisfondturbo.pretloan.pro.ProDialog
import com.sikadwafastdaisfondturbo.pretloan.ui.PrivacyPolicyActivity
import com.sikadwafastdaisfondturbo.pretloan.ui.user.activity.AccountListActivity
import com.sikadwafastdaisfondturbo.pretloan.ui.user.activity.FeedbackActivity
import com.sikadwafastdaisfondturbo.pretloan.ui.user.activity.UserLoginActivity
import com.sikadwafastdaisfondturbo.pretloan.utils.config.Configs
import com.sikadwafastdaisfondturbo.pretloan.utils.event.EventType
import com.sikadwafastdaisfondturbo.pretloan.utils.http.HttpHelper
import com.sikadwafastdaisfondturbo.pretloan.utils.sp.SPUtils

/**
 * Author : wwdeng
 * Date : 2024/5/24 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 个人中心
 */
class PersonalDialog: ProDialog {
    private lateinit var binding: DialogPersonalBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun initWindow() {
        val params = window!!.attributes
        params.width = App.getPhoneWidth()
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window!!.setGravity(Gravity.BOTTOM)
    }

    override fun getRootView(): View {
        binding = DialogPersonalBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        binding.textMobile.text = Configs.getSPString(Configs.SPKEY.SIKADWA_MOBILE)
        //获取版本名称
        val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
        binding.textVersion.text = String.format(context.getString(R.string.personal_version), packageInfo.versionName)
    }

    override fun initListener() {
        setOnClickListener(R.id.imgClose)
        setOnClickListener(R.id.accountLayout)
        setOnClickListener(R.id.feedbackLayout)
        setOnClickListener(R.id.rateUsLayout)
        setOnClickListener(R.id.shareLayout)
        setOnClickListener(R.id.btnLoginOut)
        setOnClickListener(R.id.textPrivacy)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        when (v!!.id) {
            R.id.accountLayout -> {//账户
                val intent = Intent(context, AccountListActivity::class.java)
                context.startActivity(intent)
            }
            R.id.feedbackLayout -> {//意见反馈
                val intent = Intent(context,FeedbackActivity::class.java)
                context.startActivity(intent)
            }
            R.id.rateUsLayout -> {//打分
                if (mCallback != null){
                    mCallback!!.onRateUsClick()
                }
            }
            R.id.shareLayout -> {//分享
                onCopyAction()
            }
            R.id.btnLoginOut -> {//退出登录
                HttpHelper.addEvent(EventType.CLICK.CLICK_USER_LOGINOUT,"")
                //移除已登录状态
                SPUtils.remove(Configs.SPKEY.SIKADWA_LOGIN)
                //移除用户Token
                SPUtils.remove(Configs.SPKEY.SIKADWA_TOKEN)
                //移除用户id
                SPUtils.remove(Configs.SPKEY.SIKADWA_USERID)

                val intent = Intent(context, UserLoginActivity::class.java)
                context.startActivity(intent)
            }
            R.id.textPrivacy -> {//隐私政策
                val intent = Intent(context,PrivacyPolicyActivity::class.java)
                context.startActivity(intent)
            }
        }
    }

    /**
     * 复制
     */
    private fun onCopyAction(){
        val linkUrl = "https://play.google.com/store/apps/details?id=${AnalysisBuilder.appPckName}"
        HttpHelper.addEvent(EventType.CLICK.CLICK_USER_SHARE,linkUrl)
        // 获取剪贴板管理器
        val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        // 创建一个 ClipData 对象，将要复制的文本放入其中
        val clipData = ClipData.newPlainText("label", linkUrl)
        // 将 ClipData 对象放入剪贴板
        clipboardManager.setPrimaryClip(clipData)

        showLinkCopiedDialog()
    }

    /**
     * 分享链接复制成功
     */
    private fun showLinkCopiedDialog(){
        val linkCopiedDialog = LinkCopiedDialog(context)
        linkCopiedDialog.show()
        Handler().postDelayed({
            linkCopiedDialog.dismiss()
        },1000)
    }

    interface IDialogCallback {
        /**
         * 打分
         */
        fun onRateUsClick()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PERSONAL_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PERSONAL_DISMISS,eventRemark)
    }
}